package com.zimi.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zimi.bean.Constants;
import com.zimi.mapper.*;
import com.zimi.model.*;
import com.zimi.util.CommonUtil;
import com.zimi.util.ShiroUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WfActivityAuthorityService {

    @Autowired
    WfActivityAuthorityMapper wfActivityAuthorityMapper;
    @Autowired
    WfActivityAuthorityVOMapper wfActivityAuthorityVOMapper;
    @Autowired
    WfActivityMapper wfActivityMapper;
    @Autowired
    WfWorkflowMapper wfWorkflowMapper;
    @Autowired
    SysPostMapper sysPostMapper;
    @Autowired
    WfWorkflowVOMapper wfWorkflowVOMapper;
    @Autowired
    SysRoleUserMapper sysRoleUserMapper;
    @Autowired
    private CommonUtil commonUtil;

    public Map userTree() {
        Map map = new HashMap();
        // 调用机构树共通方法
        map.put("data", commonUtil.getOrgPostUser(new ArrayList()));
        map.put("checkedArr", new ArrayList<>());
        return map;
    }

    public void deleteByPrimaryKey(Integer id) {
        WfActivityAuthority wfActivityAuthority = new WfActivityAuthority();
        wfActivityAuthority.setId(id);

        wfActivityAuthority.deleteById();
    }


    public int insert(WfActivityAuthority record) {
        return wfActivityAuthorityMapper.insert(record);
    }


    public int insertSelective(WfActivityAuthority record) {
        record.insert();
        return record.getId();
    }


    public WfActivityAuthority selectByPrimaryKey(Integer id) {
        WfActivityAuthority wfActivityAuthority = new WfActivityAuthority();
        wfActivityAuthority.setId(id);

        return wfActivityAuthority.selectById();
    }


    public void updateByPrimaryKeySelective(WfActivityAuthority record) {
        record.updateById();
    }


    public void updateByPrimaryKey(WfActivityAuthority record) {
        record.updateById();
    }


    public List<WfActivityAuthority> selectAll(WfActivityAuthority record) {
        return record.selectList(new QueryWrapper<WfActivityAuthority>().setEntity(record));
    }

    public List<WfActivityAuthorityVO> getAllByUser() {
        List<WfActivityAuthorityVO> result = new ArrayList<>();

        // 1)当前
        Session session = ShiroUtils.getSession();
        SysPostUser sysPostUser = (SysPostUser) session.getAttribute(Constants.CURENT_POST_USER);

        if (sysPostUser != null) {
            Map map = new HashMap();

            map.put("orgId", sysPostUser.getOrgId());
            map.put("postId", sysPostUser.getPostId());
            map.put("userId", sysPostUser.getUserId());

            List<SysRoleUser> sysRoleUsers = sysRoleUserMapper.selectList(new QueryWrapper<SysRoleUser>().lambda().eq(SysRoleUser::getState, 1).eq(SysRoleUser::getUserId, sysPostUser.getUserId()));
            List<Integer> roleIds = new ArrayList<>();

            if (sysRoleUsers != null && sysRoleUsers.size() > 0) {
                for (SysRoleUser sru : sysRoleUsers) {
                    roleIds.add(sru.getRoleId());
                }
            }

            map.put("roleIds", roleIds);

            result = wfActivityAuthorityMapper.selectAuthorityByUser(map);
        }

        return result;
    }

    public int deleteByActivityId(Integer id) {
        return wfActivityAuthorityVOMapper.deleteAuthorityByCode(id);
    }

    public void saveBatch(List<WfActivityAuthority> wfActivityAuthorityList) throws Exception {
        if (wfActivityAuthorityList != null && wfActivityAuthorityList.size() > 0) {
            // 循环插入活动权限数据
            for (WfActivityAuthority wfActivityAuthority : wfActivityAuthorityList) {
                wfActivityAuthority.insert();
            }
        }
    }

    /*
     * @author: wj
     * @description: 得到所有该用户所有的流程权限数据记录
     * @date: 2019/4/3 19:34
     * @param
     * @return
     */
    public List getUserAll(SysUser sysUser) {
        Map map = new HashMap();
        //map.put("orgId", sysUser.getOrgId());
//        SysPost sysPost = sysPostMapper.selectById(sysUser.getPostId());
        /*if (sysPost != null) {
            map.put("dutyIds", sysPost.getDutyId());
        } else {
            map.put("dutyIds", new String[0]);
        }*/

/*        map.put("postId", sysUser.getPostId());
        map.put("roleId", sysUser.getRoleId());*/
        map.put("userId", sysUser.getId());
        List<WfActivityAuthorityVO> list = wfActivityAuthorityVOMapper.selectAuthorityByUser(map);
        // 定义临时键变量
        int oldKey = 0;
        WfActivityAuthorityVO wfActivityAuthority = null;
        WfWorkflowVO wfWorkflowVO = null;
        WfActivity wfActivity = null;
        List<WfActivityAuthority> result = new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (Object obj : list) {
                wfActivityAuthority = (WfActivityAuthorityVO) obj;
                if (wfActivityAuthority.getActivityId() != null && wfActivityAuthority.getActivityId() != 0) {
                    // 取得WfActivity
                    wfActivity = wfActivityMapper.selectByPrimaryKey(wfActivityAuthority.getActivityId());
                    if (wfActivity == null) continue;
                    // 取得WfWorkflow
                    wfWorkflowVO = wfWorkflowVOMapper.selectWfWorkflowVOById(wfActivity.getWfId());
                    if (wfWorkflowVO == null) continue;
                    if (wfWorkflowVO.getActiveFlag() != 1) continue;
                    wfActivityAuthority.setWfId(wfWorkflowVO.getWfId());
                    wfActivityAuthority.setModuleId(wfWorkflowVO.getModuleId());
                    wfActivityAuthority.setModuleName(wfWorkflowVO.getModuleName());
                    wfActivityAuthority.setWfName(wfWorkflowVO.getWfName());
                    if (wfActivityAuthority.getActivityId().intValue() != oldKey) {
                        result.add(wfActivityAuthority);
                        oldKey = wfActivityAuthority.getActivityId();
                    }
                }
            }
            // 释放资源
            list.clear();
            list = null;
        }
        return result;
    }
}
